package funcktion;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Objects;

public class HomePageGUI extends JFrame{

    public HomePageGUI(String role,int userId) {
        System.out.println(role);
        if (Objects.equals(role, "reader")){
            readerGUI(userId);
        }else if (Objects.equals(role, "operator")){
            operatorGUI();
        }else {
            //System.out.println("admin");
            adminGUI(userId);
        }
    }


    private void readerGUI(int userId) {
        // 创建首页GUI窗口
        setTitle("首页");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(400, 300);
        setLocationRelativeTo(null);


        // 创建功能按钮
        JButton readerInfoButton = new JButton("查询读者信息");
        JButton bookInfoButton = new JButton("查询书籍信息");

        // 为功能按钮添加事件处理程序
        readerInfoButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 跳转到查询读者信息页面
                // 执行相关操
                User GUI = new User();
                GUI.setVisible(true);
            }
        });

        bookInfoButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 跳转到查询书籍信息页面
                // 执行相关操作
                BookSearch GUI2 = new BookSearch(userId);
                GUI2.setVisible(true);
            }
        });



        // 将功能按钮添加到首页GUI页面
        JPanel buttonPanel = new JPanel();
        buttonPanel.add(readerInfoButton);
        buttonPanel.add(bookInfoButton);


        // 将按钮面板添加到首页GUI页面
        getContentPane().add(buttonPanel, BorderLayout.CENTER);

        // 显示首页GUI页面
        setVisible(true);
    }

    private void operatorGUI() {
        // 创建首页GUI窗口
        setTitle("首页");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(400, 300);
        setLocationRelativeTo(null);


        // 创建功能按钮
        JButton userManaButton = new JButton("读者操作管理");
        JButton bookManaButton = new JButton("图书操作管理");

        // 为功能按钮添加事件处理程序

        userManaButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                UserManage userManage = new UserManage();
                userManage.setVisible(true);
            }
        });

        bookManaButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                BookManage GUI3 = new  BookManage();
                GUI3.setVisible(true);
            }
        });


        // 将功能按钮添加到首页GUI页面
        JPanel buttonPanel = new JPanel();
        buttonPanel.add(userManaButton);
        buttonPanel.add(bookManaButton);

        // 将按钮面板添加到首页GUI页面
        getContentPane().add(buttonPanel, BorderLayout.CENTER);

        // 显示首页GUI页面
        setVisible(true);
    }




    private void adminGUI(int userId) {
        // 创建首页GUI窗口
        setTitle("首页");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(400, 300);
        setLocationRelativeTo(null);


        // 创建功能按钮
        JButton readerInfoButton = new JButton("查询读者信息");
        JButton bookInfoButton = new JButton("查询书籍信息");
        JButton userManaButton = new JButton("读者操作管理");
        JButton bookManaButton = new JButton("图书操作管理");

        // 为功能按钮添加事件处理程序
        readerInfoButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 跳转到查询读者信息页面
                // 执行相关操
                User GUI = new User();
                GUI.setVisible(true);
            }
        });

        bookInfoButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 跳转到查询书籍信息页面
                // 执行相关操作
                BookSearch GUI2 = new BookSearch(userId);
                GUI2.setVisible(true);
            }
        });

        userManaButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                UserManage userManage = new UserManage();
                userManage.setVisible(true);
            }
        });

        bookManaButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                BookManage GUI3 = new  BookManage();
                GUI3.setVisible(true);
            }
        });


        // 将功能按钮添加到首页GUI页面
        JPanel buttonPanel = new JPanel();
        buttonPanel.add(readerInfoButton);
        buttonPanel.add(bookInfoButton);
        JPanel buttonPanel2 = new JPanel();
        buttonPanel2.add(userManaButton);
        buttonPanel2.add(bookManaButton);

        // 将按钮面板添加到首页GUI页面
        getContentPane().add(buttonPanel, BorderLayout.NORTH);
        getContentPane().add(buttonPanel2, BorderLayout.CENTER);

        // 显示首页GUI页面
        setVisible(true);
    }
}
